/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.se2.meteocal.control;

import it.polimi.se2.meteocal.entity.Calendar;
import it.polimi.se2.meteocal.entity.Event;
import it.polimi.se2.meteocal.entity.Users;
import java.util.List;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;


/**
 *
 * @author Ugo
 */
@Stateless
public class CalendarManager {
    
    private Logger logger = Logger.getLogger(CalendarManager.class.getName());
    
    @PersistenceContext
    public EntityManager em;
    
    public void create(Calendar entity) {
        em.persist(entity);
    }

    public void edit(Calendar entity) {
        em.merge(entity);
    }

    public void remove(Calendar entity) {
        em.remove(em.merge(entity));
    }
    
      
    public Calendar findCalendarByUser(Users u){
        
        return em.createNamedQuery(Calendar.findByUser, Calendar.class)
                .setParameter("user", u)
                .getSingleResult();
    }
    
    public List<Event> findEventsInCalendar(Calendar calendar){
        return em.createNamedQuery(Event.findByCalendar, Event.class)
                .setParameter("calendar", calendar)
                .getResultList();  
    }
    
    public List<Event> findPublicEventsInCalendar(Calendar calendar){
        return em.createNamedQuery(Event.findPublicByCalendar, Event.class)
                .setParameter("calendar", calendar)
                .getResultList();  
    }
    
    public Users findUserByCalendar(Long id){
         return em.createNamedQuery(Calendar.findById, Users.class)
                .setParameter("id", id)
                .getSingleResult();
    }
  
}
